package com.dsp.spectrogram;

import java.util.Locale;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceFragment;

public class PreferencesFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
	
	private Monitor main;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		main = Settings.getCallbackInterface();
 		addPreferencesFromResource(R.xml.prefs);
 		
 		Preference preference = findPreference(getString(R.string.prefPlayback));
 		preference.setSummary("Current: " + (Settings.playback?"Enabled":"Disabled"));
 		
 		preference = findPreference(getString(R.string.prefOutputStream));
 		preference.setSummary("Current: " + Settings.getOutput());

        preference = findPreference(getString(R.string.prefSamplingFreq));
        ((ListPreference)preference).setEntries(Settings.samplingRates);
        ((ListPreference)preference).setEntryValues(Settings.samplingRateValues);
        preference.setSummary("Current: " + Settings.Fs + "Hz");
        
        preference = findPreference(getString(R.string.prefBlockSize));
        preference.setSummary("Current: " + Settings.blockSize + " samples");
        preference.setOnPreferenceChangeListener(preferenceChange);
        
        preference = findPreference(getString(R.string.prefDelay));
        preference.setSummary("Current: " + Settings.D + " samples");
        preference.setOnPreferenceChangeListener(preferenceChange);
        
        preference = findPreference(getString(R.string.prefAlpha));
        preference.setSummary("Current: " + Settings.a);
        preference.setOnPreferenceChangeListener(preferenceChange);
        
        preference = findPreference(getString(R.string.prefDebug));
        preference.setSummary("Current: " + Settings.getDebug());
	}

	public void onResume() {
		super.onResume();
		getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
	}
	
	public void onPause() {
		getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
		super.onPause();
	}
	
	private OnPreferenceChangeListener preferenceChange = new OnPreferenceChangeListener() {
		public boolean onPreferenceChange(Preference preference, Object changedValue) {
			String testString = changedValue.toString().trim();
			if(testString == "") {
				return false;
			} else {
				try {
					Float.parseFloat(testString);
					return true;
				} catch(NumberFormatException e){}
				return false;
			}
		}
	};

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		
		Preference preference = findPreference(key);
		if (key.equals(getString(R.string.prefPlayback))) {
			boolean setting = sharedPreferences.getBoolean(key, false);
			if(Settings.setPlayback(setting)) {
				String[] result = {"Playback disabled.", "Playback enabled for " + Settings.getOutput().toLowerCase(Locale.US) + " output."};
				preference.setSummary("Current: " + (Settings.playback?"Enabled":"Disabled"));
				preference = findPreference(getString(R.string.prefOutputStream));
		 		preference.setSummary("Current: " + Settings.getOutput()); 	
		 		main.notify(result[Settings.playback?1:0]);
			}
		} else if (key.equals(getString(R.string.prefOutputStream))) {
			int setting = Integer.parseInt(sharedPreferences.getString(key, "2"));
			if(Settings.setOutput(setting)) {
				preference.setSummary("Current: " + Settings.getOutput());
				String[] result = {"Playback set to original output.", "Playback set to filtered output."};
				main.notify(result[Settings.output?1:0]);
			}
		} else if (key.equals(getString(R.string.prefSamplingFreq))) {
			int setting = Integer.parseInt(sharedPreferences.getString(key, "8000"));
			if(Settings.setSamplingFrequency(setting)) {
				preference.setSummary("Current: " + Settings.Fs + "Hz");
				main.notify("Sampling rate set to " + Settings.Fs + "Hz.");
			}
		} else if (key.equals(getString(R.string.prefBlockSize))) {
			int setting = Integer.parseInt(sharedPreferences.getString(key, "256"));
			if(Settings.setBlockSize(setting)) {
				preference.setSummary("Current: " + Settings.blockSize + " samples");
				main.notify("Frame size set to " + Settings.blockSize + " samples.");
			}
		} else if (key.equals(getString(R.string.prefDelay))) {
			int setting = Integer.parseInt(sharedPreferences.getString(key, "0"));
			if(Settings.setDelay(setting)) {
				preference.setSummary("Current: " + Settings.D + " samples");
				main.notify("Difference equation delay set to " + Settings.D + " samples.");
			}
		} else if (key.equals(getString(R.string.prefAlpha))) {
			float setting = Float.parseFloat(sharedPreferences.getString(key, "1.0"));
			if(Settings.setAlpha(setting)) {
				preference.setSummary("Current: " + Settings.a);
				main.notify("Difference equation alpha set to " + Settings.a);
			}
		} else if (key.equals(getString(R.string.prefDebug))) {
			int setting = Integer.parseInt(sharedPreferences.getString(key, "4"));
			if(Settings.setDebugLevel(setting)) {
				preference.setSummary("Current: " + Settings.getDebug());
				String[] result = {"All debug outputs enabled.", "Text file output enabled.", "PCM ouput enabled.", "Debug ouput disabled."};
				main.notify(result[Settings.debugLevel-1]);
			}
		}
	}
}